package com.bjpowernode.boss.mvc.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjpowernode.boss.enums.RespInfo;
import com.bjpowernode.boss.enums.UserStatus;
import com.bjpowernode.boss.exception.PowerException;
import com.bjpowernode.boss.mvc.models.query.UserEditQuery;
import com.bjpowernode.boss.mvc.service.UserService;
import com.bjpowernode.boss.persist.domain.UserAccount;
import com.bjpowernode.boss.persist.domain.UserInfo;
import com.bjpowernode.boss.persist.mapper.UserAccountMapper;
import com.bjpowernode.boss.persist.mapper.UserInfoMapper;
import com.bjpowernode.boss.utils.IdUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserInfoMapper userInfoMapper;

    @Resource
    private UserAccountMapper userAccountMapper;

    @Override
    public Page<UserInfo> getUserList(Integer pageNo, Integer pageSize) {
        return userInfoMapper.selectPage(Page.of(pageNo, pageSize)
                                  , new QueryWrapper<UserInfo>().orderByDesc("create_time"));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized void edit(UserEditQuery userEditQuery) {
        String userNo = userEditQuery.getUserNo();
        if (StrUtil.isBlank(userNo)){

            Long count = userInfoMapper.selectCount(new QueryWrapper<UserInfo>()
                    .eq("login_name", userEditQuery.getLoginName()));
            if (count > 0) {
                throw new PowerException(RespInfo.USER_EXIST_ERROR);
            }
            addUserInfoAndAccount(userEditQuery);

        } else {
            checkExist(userNo);
            userInfoMapper.update(new UpdateWrapper<UserInfo>()
                    .eq("user_no", userNo)
                    .set("phone", userEditQuery.getPhone())
                    .set("shop_name", userEditQuery.getShopName())
                    .set("shop_scope", userEditQuery.getShopScope())
                    .set("user_email", userEditQuery.getUserEmail()));

        }

    }

    private void checkExist(String userNo) {
        UserInfo userInfo = queryByUserNo(userNo);
        if (userInfo == null) {
            throw new PowerException(RespInfo.USER_NOT_EXIST_ERROR);
        }
    }

    private void addUserInfoAndAccount(UserEditQuery userEditQuery) {
        UserInfo userInfo = new UserInfo();
        userInfo.setId(IdUtil.buildId());
        userInfo.setUserNo(IdUtil.buildUserNo());
        userInfo.setLoginName(userEditQuery.getLoginName());
        userInfo.setAccountNo(IdUtil.buildAccountNo());
        userInfo.setPhone(userEditQuery.getPhone());
        userInfo.setPassword(IdUtil.uuid());
        userInfo.setPayPwd(IdUtil.uuid());
        userInfo.setShopName(userEditQuery.getShopName());
        userInfo.setShopScope(userEditQuery.getShopScope());
        userInfo.setUserEmail(userEditQuery.getUserEmail());
        userInfo.setStatus(UserStatus.ENABLE.name());
        userInfo.setCreateTime(new Date());
        userInfo.setRemark("创建离线商户");
        userInfoMapper.insert(userInfo);

        UserAccount userAccount = new UserAccount();
        userAccount.setId(IdUtil.buildId());
        userAccount.setUserNo(userInfo.getUserNo());
        userAccount.setAccountNo(userInfo.getAccountNo());
        userAccount.setBalance(new BigDecimal(0));
        userAccount.setTotalIncome(new BigDecimal(0));
        userAccount.setTotalExpend(new BigDecimal(0));
        userAccount.setStatus(UserStatus.DISABLE.name());
        userAccount.setRemark("创建离线商户时创建账户");
        userAccount.setEditTime(new Date());
        userAccountMapper.insert(userAccount);
    }

    @Override
    public UserInfo queryByUserNo(String userNo) {
        return userInfoMapper.selectOne(new QueryWrapper<UserInfo>().eq("user_no", userNo));
    }

    @Override
    public void changeStatus(UserEditQuery userEditQuery) {
        checkExist(userEditQuery.getUserNo());
        String status = UserStatus.DISABLE.name();

        if ("禁用".equals(userEditQuery.getStatus())) {
            status = UserStatus.ENABLE.name();
        }
        userInfoMapper.update(new UpdateWrapper<UserInfo>()
                .eq("user_no", userEditQuery.getUserNo())
                .set("status", status));
    }

    @Override
    public List<UserInfo> matchShopName(String shopName) {
        return userInfoMapper.selectList(new QueryWrapper<UserInfo>().like("shop_name", shopName));
    }
}
